'
' NOTE: ColourSettings uses British Spelling of COLOUR to ensure no conflict with COLOR drawing object
'
Imports System.Drawing

Namespace TressleWorks.Modules.ThermoChart

    Public Class ColourSettings
        Private sColour As String
        Private sColour2 As String
        Private sColour3 As String
        Private sSettingString As String

        Private iColourCount As Integer

        Private iAngle As Integer   ' -360 to 360

        Private oColour As Color
        Private oColour2 As Color
        Private oColour3 As Color

        Public Sub New()
            Call Initialize()
        End Sub

        Public Sub New(ByVal psColourSetting As String)
            Call Initialize()
            Call Parse(psColourSetting)
        End Sub

        Public Sub Initialize()
            sColour = String.Empty
            sColour2 = String.Empty
            sColour3 = String.Empty

            iColourCount = 0
        End Sub

        Public ReadOnly Property SettingString() As String
            Get
                Return sSettingString
            End Get
        End Property

        Public Property Colour() As String
            Get
                Return sColour
            End Get
            Set(ByVal value As String)
                sColour = value.Trim
                oColour = ToColorFromString(sColour)
            End Set
        End Property

        Public Property Colour2() As String
            Get
                Return sColour2
            End Get
            Set(ByVal value As String)
                sColour2 = value.Trim
                oColour2 = ToColorFromString(sColour2)
            End Set
        End Property

        Public Property Colour3() As String
            Get
                Return sColour3
            End Get
            Set(ByVal value As String)
                sColour3 = value.Trim
                oColour3 = ToColorFromString(sColour3)
            End Set
        End Property

        Public ReadOnly Property [Color]() As Color
            Get
                Return oColour
            End Get
        End Property

        Public ReadOnly Property [Color2]() As Color
            Get
                Return oColour2
            End Get
        End Property

        Public ReadOnly Property [Color3]() As Color
            Get
                Return oColour3
            End Get
        End Property


        Public ReadOnly Property ColourCount() As Integer
            Get
                Return iColourCount
            End Get
        End Property

        Public Property Angle() As Integer
            Get
                Return iAngle
            End Get
            Set(ByVal Value As Integer)
                If Math.Abs(Value) > 360 Then
                    Value = 0
                End If
                iAngle = Value
            End Set
        End Property


        Public Sub Parse(ByVal psColourSettings As String)

            Dim sTemp As String = String.Empty

            sColour = "Black"
            sColour2 = String.Empty
            sColour3 = String.Empty
            iColourCount = 1

            If psColourSettings.Length = 0 Then
                Exit Sub
            End If

            If psColourSettings.StartsWith(";") Then
                Exit Sub
            End If

            'save it away
            sSettingString = psColourSettings

            Dim sSections() As String = Split(psColourSettings, ",")

            'format: color[:color2[:color3]], angle

            'color
            If sSections(0).Length > 0 Then
                sTemp = sSections(0)
                If sTemp.Contains(":") Then
                    Dim sColours() As String = sTemp.Split(":")
                    If sColours.Length > 0 Then
                        Colour = sColours(0).Trim
                        iColourCount = 1
                        If sColours.Length > 1 Then
                            Colour2 = sColours(1).Trim
                            iColourCount = 2
                            If sColours.Length > 2 Then
                                Colour3 = sColours(2).Trim
                                iColourCount = 3
                            End If
                        End If
                    End If
                Else
                    Colour = sTemp
                    iColourCount = 1
                End If
            End If

            'angle
            If sSections.Length > 1 Then
                iAngle = Val(sSections(1))
            End If

        End Sub


        Private Function ToColorFromString(ByVal psColourString As String) As Color

            If psColourString.Length = 0 Then
                Return Color.White
            End If

            If psColourString.StartsWith("&H") Then
                Dim Num As Integer = Convert.ToInt32(psColourString, 16)
                Dim R As Integer = Num \ &H10000
                Dim G As Integer = (Num Mod &H100) \ &H100
                Dim B As Integer = Num Mod &H100
                Return Color.FromArgb(R, G, B)
            End If

            If psColourString.StartsWith("#") Then
                Return ColorTranslator.FromHtml(psColourString)
            End If

            Return Color.FromName(psColourString.Trim)

        End Function

    End Class
End Namespace
